package com.zh.note.huawei.onehundred.TLV编码解码;

/**
 * https://dream.blog.csdn.net/article/details/129191146
 */

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);

        // String tagStr = s.nextLine();
        String tagStr = "31";

        // String streamStr = s.nextLine();
        String streamStr = "32 01 00 AE 90 02 00 01 02 30 03 00 AB 32 31 31 02 00 32 33 33 01 00 CC ";
        Map<String, String> map = parseStream(streamStr);
        String inner = map.get(tagStr);
        System.out.println(inner);
    }

    static Map<String, String> parseStream(String streamStr) {
        Map<String, String> map = new HashMap<>();

        String[] input = streamStr.split(" ");
        int index = 0;
        while (input.length > index) {
            String tagTemp = input[index];
            String lengthStr = input[index + 2] + input[index + 1];
            index += 3;
            //16进制转化10进制
            int length = Integer.parseInt(lengthStr, 16);
            StringBuilder sb = new StringBuilder();
            for (int i = index; i < index + length; ++i) {
                sb.append(input[i]).append(" ");
            }
            map.put(tagTemp, sb.toString().trim());
            index += length;
        }
        return map;
    }
}

